package com.bawei.module_live.version2

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bawei.lib_common.mvi.base.BaseMVIFragment
import com.bawei.module_live.databinding.FragmentLive2Binding
import com.tencent.rtmp.TXLiveConstants
import com.tencent.rtmp.TXLivePlayer

//腾讯云拉流
class LiveFragment2 : BaseMVIFragment() {
    private val binding: FragmentLive2Binding by lazy {
        FragmentLive2Binding.inflate(layoutInflater)
    }
    private lateinit var mLivePlayer: TXLivePlayer
    override fun generateViewModel() {

    }

    override fun lazyLoad() {
    }

    override fun getFragmentRootView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return binding.root
    }

    override fun handleState() {

    }

    override fun initView() {
        super.initView()
         mLivePlayer = TXLivePlayer(requireActivity());
        //关键 player 对象与界面 view
        mLivePlayer.setPlayerView(binding.videoView);

        val flvUrl = "http://3891.liveplay.myqcloud.com/live/3891_user_994f8711_0db7.flv"
        mLivePlayer.startLivePlay(flvUrl,TXLivePlayer.PLAY_TYPE_LIVE_FLV)


        // 设置填充模式
        mLivePlayer.setRenderMode(TXLiveConstants.RENDER_MODE_ADJUST_RESOLUTION);
        //RENDER_MODE_FULL_FILL_SCREEN  将图像等比例铺满整个屏幕，多余部分裁剪掉，此模式下画面不会留黑边，但可能因为部分区域被裁剪而显示不全。
        //RENDER_MODE_ADJUST_RESOLUTION 将图像等比例缩放，适配最长边，缩放后的宽和高都不会超过显示区域，居中显示，画面可能会留有黑边。
        // 设置画面渲染方向
        mLivePlayer.setRenderRotation(TXLiveConstants.RENDER_ROTATION_LANDSCAPE);
        //RENDER_ROTATION_PORTRAIT 正常播放（Home 键在画面正下方）
        //RENDER_ROTATION_LANDSCAPE 画面顺时针旋转 270 度（Home 键在画面正左方）


    }

    override fun onDestroy() {
        super.onDestroy()
        mLivePlayer.stopPlay(true); // true 代表清除最后一帧画面
        binding.videoView.onDestroy();
    }

}